package wondersgroup.hsswidget.dispatch;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

/**
 * Created by Administrator on 2016/12/29.
 */

public class ViewParent extends LinearLayout {

    ChildScrollView mChildScrollView;
    boolean isChildTop = true;
    boolean intercept = false;
    public ViewParent(Context context) {
        super(context);
    }

    public ViewParent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ViewParent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mChildScrollView = (ChildScrollView) getChildAt(0);
        mChildScrollView.setListener(new ChildScrollView.ScollViewTopListener() {
            @Override
            public void checIsTop(boolean flag) {
                isChildTop = flag;
            }
        });
    }
    float downY = 0,moveY = 0;
    int moveDelta = 0,i=0;
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {



        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                downY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                moveY = ev.getY();
                if ((moveY - downY) >0){
                    intercept = true;
                }else if ((moveY - downY) <0){
                    intercept = false;
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return intercept;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float y =  event.getY();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                downY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:

                moveY = y;
                if (isChildTop){//记录第一次开始move的y
                    downY = y;
                    isChildTop =false;
                }
                if (intercept && (moveY - downY)>0){
                    moveDelta = (int) (moveY - downY);
                    i+=moveDelta;
                    layout(getLeft(),getTop()+ moveDelta,getRight(),getBottom()+moveDelta);
                }
                break;
            case MotionEvent.ACTION_UP:
                layout(getLeft(),getTop()-i,getRight(),getBottom()-i);
                i = 0;
                intercept = false;

                break;
        }
        return true;
    }
}
